public class DatabaseDriverSimulation {

    // ===================== 抽象层 ======================
    interface DBFactory {
        DBConnection createConnection();
        DBStatement createStatement();
    }

    interface DBConnection {
        void connect();
    }

    interface DBStatement {
        void execute(String sql);
    }

    // ===================== MySQL 实现 ======================
    public static class MySQLFactory implements DBFactory {
        @Override
        public DBConnection createConnection() {
            return new MySQLConnection();
        }

        @Override
        public DBStatement createStatement() {
            return new MySQLStatement();
        }
    }

    public static class MySQLConnection implements DBConnection {
        @Override
        public void connect() {
            System.out.println("MySQL 数据库连接成功！");
        }
    }

    public static class MySQLStatement implements DBStatement {
        @Override
        public void execute(String sql) {
            System.out.println("MySQL 执行 SQL：" + sql);
        }
    }

    // ===================== Oracle 实现 ======================
    public static class OracleFactory implements DBFactory {
        @Override
        public DBConnection createConnection() {
            return new OracleConnection();
        }

        @Override
        public DBStatement createStatement() {
            return new OracleStatement();
        }
    }

    public static class OracleConnection implements DBConnection {
        @Override
        public void connect() {
            System.out.println("ORACLE 数据库连接成功！");
        }
    }

    public static class OracleStatement implements DBStatement {
        @Override
        public void execute(String sql) {
            System.out.println("ORACLE 执行 SQL：" + sql);
        }
    }

    // ===================== DBManager ======================
    static class DBManager {
        private static DBFactory factory;

        // 模拟加载驱动
        public static void loadDriver(String className) {
            System.out.println("已加载 " + className);
            // 判断不同的数据库类型并加载对应的工厂
            if (className.equals("com.abc.homework1.MySQLFactory")) {
                factory = new MySQLFactory();
            } else if (className.equals("com.abc.homework1.OracleFactory")) {
                factory = new OracleFactory();
            }
        }

        public static DBFactory getFactory() {
            return factory;
        }
    }

    // ===================== main ======================
    public static void main(String[] args) {


        DBManager.loadDriver("com.abc.homework1.OracleFactory");

        // DBManager.loadDriver("com.abc.homework1.OracleFactory");

        // 获取驱动工厂
        DBFactory factory = DBManager.getFactory();

        // 创建连接 & 执行 SQL
        DBConnection connection = factory.createConnection();
        connection.connect();

        DBStatement stmt = factory.createStatement();
        stmt.execute("SELECT * FROM student");
    }
}
